package com.sjq.yqgl.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import com.sjq.yqgl.dao.YqbsDao;
import com.sjq.common.dao.GenericDao;
import com.sjq.common.util.PageResults;
import com.sjq.yqgl.entity.Yqbs;

@Repository
public class YqbsDaoImpl extends GenericDao<Yqbs> implements YqbsDao {

	 @Override
	 public PageResults<?> selectYqbsByPage(int pageNo, int pageSize,Yqbs yqbs){
	 	StringBuilder paramSql = new StringBuilder();
		List params = new ArrayList();
		//查询条件
		/**/
		if (yqbs.getXxzjbh() != null&& !(yqbs.getXxzjbh().equals(""))) {
			paramSql.append(" and xxzjbh=? ");
			params.add(yqbs.getXxzjbh().trim());
		}
		if (yqbs.getGlrwid() != null&& !(yqbs.getGlrwid().equals(""))) {
			paramSql.append(" and glrwid=? ");
			params.add(yqbs.getGlrwid().trim());
		}
		if (yqbs.getXm() != null&& !(yqbs.getXm().equals(""))) {
			paramSql.append(" and instr(xm,?)>0 ");
			params.add(yqbs.getXm().trim());
		}
		if (yqbs.getStzk() != null&& !(yqbs.getStzk().equals(""))) {
			paramSql.append(" and instr(stzk,?)>0 ");
			params.add(yqbs.getStzk().trim());
		}
		if (yqbs.getHcfs() != null&& !(yqbs.getHcfs().equals(""))) {
			paramSql.append(" and instr(hcfs,?)>0 ");
			params.add(yqbs.getHcfs().trim());
		}
		
		if (yqbs.getSjhm() != null&& !(yqbs.getSjhm().equals(""))) {
			paramSql.append(" and sjhm=? ");
			params.add(yqbs.getSjhm().trim());
		}
		if (yqbs.getTbsj_s() != null&& !(yqbs.getTbsj_s().equals(""))) {
			paramSql.append(" and (hssj>?  or hssj=?)");
			params.add(yqbs.getTbsj_s().trim());
			params.add(yqbs.getTbsj_s().trim());
		}
		if (yqbs.getTbsj_e() != null&& !(yqbs.getTbsj_e().equals(""))) {
			paramSql.append(" and (hssj<?  or hssj=?) ");
			params.add(yqbs.getTbsj_e().trim());
			params.add(yqbs.getTbsj_e().trim());
		}
		
		if (yqbs.getDjrjh() != null&& !(yqbs.getDjrjh().equals(""))) {
			paramSql.append(" and instr(djrjh,?)>0 ");
			params.add(yqbs.getDjrjh().trim());
		}
		
		
		
		String sql_fenye="select sfhm,XXZJBH,XH,XM,SJHM,HJDZ,XZJD,HCFS,HSSJ,DQWZ,STZK,FHCS,SQBBRY,LXDH,SQMJ,SQMJLXDH,to_char(DJSJ,'yyyy-MM-dd hh24:mi:ss') from T_YQ_XXBS where DJBGBS <>3 " + paramSql.toString() ;
		PageResults<Map<String, String>> results = new PageResults<Map<String, String>>();
		StringBuilder pageSql = new StringBuilder()
				.append("select * from ")
				.append(" (select tmp_1.*, rownum as rowindex from ("+sql_fenye+") tmp_1 ) tmp_2 ")
				.append(" where rowindex between ")
				.append(((pageNo-1)*pageSize + 1))
				.append(" and ")
				.append(((pageNo - 1) * pageSize + pageSize));
		List<Map<String, String>> list=new ArrayList<Map<String, String>>();
		try {
			list = this.getListBySql(pageSql.toString(),params.toArray());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql="select count(1) as num from T_YQ_XXBS where DJBGBS <>3  "+paramSql.toString()+" ";
		SqlRowSet sqlRowSet = this.getJdbcTemplate().queryForRowSet(sql,params.toArray());
		int count = 0;
		while(sqlRowSet.next()){
			count = sqlRowSet.getInt("num");
		}
		results.setTotalCount(count);
        results.setPageNo(pageNo);
        results.setPageSize(pageSize);
		results.setResults(list);
		return results;	
	 }
	 @Override
	 public int addYqbs(Yqbs yqbs){
	 	String sql="insert into T_YQ_XXBS ( DJDW,DJDWMC,DJRJH,glrwid,sfhm,xxzjbh,xh,xm,sjhm,hjdz,xzjd,hcfs,hssj,dqwz,stzk,fhcs,sqbbry,lxdh,sqmj,sqmjlxdh) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)  ";
	 	Object [] params={yqbs.getDjdw(),yqbs.getDjdwmc(),yqbs.getDjrjh(),yqbs.getGlrwid(),yqbs.getSfhm(),yqbs.getXxzjbh(),yqbs.getXh(),yqbs.getXm(),yqbs.getSjhm(),yqbs.getHjdz(),yqbs.getXzjd(),yqbs.getHcfs(),yqbs.getHssj(),yqbs.getDqwz(),yqbs.getStzk(),yqbs.getFhcs(),yqbs.getSqbbry(),yqbs.getLxdh(),yqbs.getSqmj(),yqbs.getSqmjlxdh()};
	 	return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	 @Override
	 public int deleteYqbs(String yqbsId){
		 String sql="update T_YQ_XXBS set djbgbs=3 where glrwid=?";
		 Object [] params={ yqbsId};
		 return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	  @Override
	 public int updateYqbs(Yqbs yqbs){
	 	String sql="update T_YQ_XXBS set XXZJBH=?,XH=?,XM=?,SJHM=?,HJDZ=?,XZJD=?,HCFS=?,HSSJ=?,DQWZ=?,STZK=?,FHCS=?,SQBBRY=?,LXDH=?,SQMJ=?,SQMJLXDH=?,DJSJ=?  where xxzjbh=?";
	 	Object [] params={yqbs.getXxzjbh(),yqbs.getXh(),yqbs.getXm(),yqbs.getSjhm(),yqbs.getHjdz(),yqbs.getXzjd(),yqbs.getHcfs(),yqbs.getHssj(),yqbs.getDqwz(),yqbs.getStzk(),yqbs.getFhcs(),yqbs.getSqbbry(),yqbs.getLxdh(),yqbs.getSqmj(),yqbs.getSqmjlxdh(),yqbs.getDjsj()};
	 	return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	 @Override
	 public List<Map<String, String>> findYqbsByListAll(Yqbs yqbs){
		StringBuilder paramSql = new StringBuilder();
		List params = new ArrayList();
		//查询条件
		if (yqbs.getXxzjbh() != null&& !(yqbs.getXxzjbh().equals(""))) {
			paramSql.append(" and xxzjbh=? ");
			params.add(yqbs.getXxzjbh().trim());
		}
		String sql="select XXZJBH,XH,XM,SJHM,HJDZ,XZJD,HCFS,HSSJ,DQWZ,STZK,FHCS,SQBBRY,LXDH,SQMJ,SQMJLXDH,to_char(DJSJ,'yyyy-MM-dd hh24:mi:ss') from T_YQ_XXBS where DJBGBS <>3  "+ paramSql.toString() ;
		List<Map<String, String>> list=new ArrayList<Map<String, String>>();
		try {
			list = this.getListBySql(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

}